package net.googlecode.demenkov.task5;

import net.googlecode.demenkov.task5.calculator.Calculator;
import net.googlecode.demenkov.task5.calculator.SalaryCalculatorFactory;
import net.googlecode.demenkov.task5.model.Boss;
import net.googlecode.demenkov.task5.model.Employee;
import net.googlecode.demenkov.task5.model.Manager;
import net.googlecode.demenkov.task5.report.SortedOut;

import java.math.BigDecimal;
import java.util.*;

/**
 * Main application for creating collection of persons and print reports.
 */
public class SalaryApp {
    private static final BigDecimal BOSS_TAX = new BigDecimal(500);
    private static final BigDecimal MANAGER_TAX = new BigDecimal(250);
    private static final BigDecimal EMPLOYEE_TAX = new BigDecimal(125);

    public static void main(String[] args) {
        SortedOut out = new SortedOut();
        Manager boss = createBoss();
        Manager manager1 = createManager();
        Manager manager2 = createManagerTwo();
        Manager manager3 = createManagerThree();
        Manager employee1 = createEmployee();
        Manager employee2 = createEmployeeTwo();
        if (args.length > 0) {
            if (args[0].equals("code")) {
                out.codeSortedOut(createCollection(boss, manager1, employee1, manager2, manager3, employee2));
            } else {
                if (args[0].equals("name")) {
                    out.nameSortedOut(createCollection(boss, manager1, employee1, manager2, manager3, employee2));
                } else {
                    if (args[0].equals("salary")) {
                        out.salarySortedOut(createCollection(boss, manager1, employee1, manager2, manager3, employee2));
                    } else {
                        System.out.println("Wrong parametr.");

                    }
                }
            }
        } else {
            for (Manager x : createCollection(boss, manager1, employee1, manager2, manager3, employee2)) {
                System.out.println(x);
            }
        }
    }


    private static Collection<Manager> createCollection(Manager... employees) {
        Collection<Manager> stuff = new ArrayList<Manager>();

        for (Manager x : employees) {
            boolean unic = true;
            Iterator<Manager> it = stuff.iterator();
            while (it.hasNext()) {
                if (it.next().getPersonalID() == x.getPersonalID()) {
                    unic = false;
                }
            }
            if (unic) {
                stuff.add(x);
            } else {
                //System.out.println("Employee with personal ID = " + x.getPersonalID() + " is already exists.");
            }
        }
        return stuff;
    }


    private static BigDecimal calculateSalary(Manager person, BigDecimal tax) {
        Calculator salaryCalculator = SalaryCalculatorFactory.getCalculator(person);
        salaryCalculator.setTax(tax);
        return salaryCalculator.calculateSalary(person);
    }


    public static Manager createManager() {
        Manager manager = new Manager();
        manager.setPersonalID("02482");
        manager.setFirstName("Ivan");
        manager.setLastName("Ivanov");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(false);
        manager.setRate(new BigDecimal(25));
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

    public static Boss createBoss() {
        Boss boss = new Boss();
        boss.setPersonalID("00001");
        boss.setFirstName("Petr");
        boss.setLastName("Petrov");
        boss.setAge(35);
        boss.setBonusAmount(new BigDecimal(1000));
        boss.setRate(new BigDecimal(50));
        boss.setSalary(calculateSalary(boss, BOSS_TAX));

        return boss;
    }

    public static Employee createEmployee() {
        Employee employee = new Employee();
        employee.setPersonalID("12345");
        employee.setFirstName("Semen");
        employee.setLastName("Semenov");
        employee.setAge(55);
        employee.setBonus(true);
        employee.setBonusAmount(new BigDecimal(1500));
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(8));
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

    public static Employee createEmployeeTwo() {
        Employee employee = new Employee();
        employee.setPersonalID("47283");
        employee.setFirstName("Afanasiy");
        employee.setLastName("afanasievich");
        employee.setAge(39);
        employee.setBonus(true);
        employee.setBonusAmount(new BigDecimal(2000));
        employee.setPerHour(true);
        employee.setRate(new BigDecimal(9));
        employee.setSalary(calculateSalary(employee, EMPLOYEE_TAX));

        return employee;
    }

    public static Manager createManagerTwo() {
        Manager manager = new Manager();
        manager.setPersonalID("02483");
        manager.setFirstName("Ivan");
        manager.setLastName("Ivanovich");
        manager.setAge(30);
        manager.setPerHour(true);
        manager.setBonus(false);
        manager.setRate(new BigDecimal(25));
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

    public static Manager createManagerThree() {
        Manager manager = new Manager();
        manager.setPersonalID("83721");
        manager.setFirstName("Andrey");
        manager.setLastName("Andrienko");
        manager.setAge(45);
        manager.setPerHour(true);
        manager.setBonus(true);
        manager.setBonusAmount(new BigDecimal(1000));
        manager.setRate(new BigDecimal(30));
        manager.setSalary(calculateSalary(manager, MANAGER_TAX));

        return manager;
    }

}
